package com.piggy.logi.sys.controller;

import com.piggy.common.core.domain.R;
import com.piggy.common.core.web.controller.BaseController;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.common.log.annotation.LogiLog;
import com.piggy.common.log.enums.BusinessType;
import com.piggy.common.satoken.utils.SecurityUtils;
import com.piggy.logi.sys.bo.LogiMarginAddBo;
import com.piggy.logi.sys.bo.LogiMarginEditBo;
import com.piggy.logi.sys.bo.LogiMarginQueryBo;
import com.piggy.logi.sys.bo.LogiMarginUserQueryBo;
import com.piggy.logi.sys.service.ILogiMarginService;
import com.piggy.logi.sys.vo.LogiMarginUserVo;
import com.piggy.logi.sys.vo.LogiMarginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 保证金Controller
 *
 * @author fengxy
 * @date 2023/10/17 21:28
 **/
@Api(value = "保证金控制器", tags = {"保证金"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys/margin")
public class LogiMarginController extends BaseController {

    private final ILogiMarginService iLogiMarginService;

    /**
     * 查询保证金列表
     */
    @ApiOperation("查询保证金列表")
    //@SaCheckPermission("sys:margin:list")
    @PostMapping("/list")
    public TableDataInfo<LogiMarginVo> list(@Validated @RequestBody LogiMarginQueryBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        return iLogiMarginService.queryPageList(bo);
    }

    /**
     * 获取保证金详情
     */
    @ApiOperation("获取保证金详情")
    //@SaCheckPermission("sys:margin:query")
    @GetMapping("/{id}")
    public R<LogiMarginVo> queryById(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        return R.ok(iLogiMarginService.queryById(id));
    }

    /**
     * 缴纳保金
     **/
    @ApiOperation("缴纳保金")
    //@SaCheckPermission("sys:margin:add")
    @LogiLog(title = "保证金管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Object> add(@Validated @RequestBody LogiMarginAddBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        iLogiMarginService.insertByAddBo(bo);
        return R.ok();
    }

    /**
     * 退回保金
     **/
    @ApiOperation("退回保金")
    //@SaCheckPermission("sys:margin:refund")
    @LogiLog(title = "保证金管理", businessType = BusinessType.UPDATE)
    @PostMapping("/refund")
    public R<Object> refund(@Validated @RequestBody LogiMarginEditBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        iLogiMarginService.refund(bo);
        return R.ok();
    }

    /**
     * 冻结保金
     **/
    @ApiOperation("冻结保金")
    //@SaCheckPermission("sys:margin:freeze")
    @LogiLog(title = "保证金管理", businessType = BusinessType.UPDATE)
    @PostMapping("/freeze/{id}")
    public R<Object> freeze(@ApiParam(value = "保证金Id") @NotNull(message = "保证金id不能为空") @PathVariable(value = "id") Long id) {
        iLogiMarginService.freeze(id);
        return R.ok();
    }

    /**
     * 解冻保金
     **/
    @ApiOperation("解冻保金")
    //@SaCheckPermission("sys:margin:thaw")
    @LogiLog(title = "保证金管理", businessType = BusinessType.UPDATE)
    @PostMapping("/thaw/{id}")
    public R<Object> thaw(@ApiParam(value = "保证金Id") @NotNull(message = "保证金id不能为空") @PathVariable(value = "id") Long id) {
        iLogiMarginService.thaw(id);
        return R.ok();
    }

    /**
     * 查询保证金用户列表
     */
    @ApiOperation("查询保证金用户列表")
    //@SaCheckPermission("sys:margin:user:list")
    @PostMapping("/user/list")
    public R<List<LogiMarginUserVo>> userList(@Validated @RequestBody LogiMarginUserQueryBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        return R.ok(iLogiMarginService.userList(bo));
    }
}
